package frameworks.business
{
	import flash.display.Loader;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	import flash.net.URLLoader;
	import flash.net.URLLoaderDataFormat;
	import flash.net.URLRequest;
	import flash.net.URLRequestMethod;
	import flash.net.URLVariables;
	import flash.system.ApplicationDomain;
	import flash.system.LoaderContext;
	import flash.utils.clearTimeout;
	import flash.utils.setTimeout;
	
	public class HttpReqService extends RequestConnect
	{
		private var _dataLoader:URLLoader = new URLLoader();
		private var _fileLoader:URLLoader = new URLLoader();
		private var _imageLoader:Loader = new Loader();
		private var _classLoader:Loader = new Loader();
		private var _dataLoaderTimer:uint;
		private var _fileLoaderTimer:uint;
		private var _imageLoaderTimer:uint;
		private var _classLoaderTimer:uint;
		
		public function HttpReqService()
		{
			_dataLoader.addEventListener(Event.COMPLETE,dataSuccessHandler);
			_dataLoader.addEventListener(IOErrorEvent.IO_ERROR,faultHandler);
			_dataLoader.addEventListener(ProgressEvent.PROGRESS,progressHandler);
			_fileLoader.addEventListener(Event.COMPLETE,fileSuccessHandler);
			_fileLoader.addEventListener(IOErrorEvent.IO_ERROR,faultHandler);
			_fileLoader.addEventListener(ProgressEvent.PROGRESS,progressHandler);
			_imageLoader.contentLoaderInfo.addEventListener(Event.COMPLETE,imageLoaderSuccessHandler);
			_imageLoader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,faultHandler);
			_imageLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS,progressHandler);
			_classLoader.contentLoaderInfo.addEventListener(Event.COMPLETE,classLoaderSuccessHandler);
			_classLoader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,faultHandler);
			_classLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS,progressHandler);
		}
		
		public function configGet():void{
			var time:uint = new Date().time;
			var get:URLVariables = new URLVariables();
			get.uname = "aaa";
			get.uid = "444";
			loadData(Setting.ConfigUrl+"&_c=Swf&_f=init&_="+time, get);
		}
		
		private function loadImage(url:String):void{
			_imageLoader.load(new URLRequest(url));
		}
		private function loadClass(url:String):void{
			var context:LoaderContext = new LoaderContext();
			context.applicationDomain = ApplicationDomain.currentDomain;
			//context.applicationDomain = new ApplicationDomain();
			_classLoader.load(new URLRequest(url),context);
		}
		private function loadData(url:String,get:URLVariables=null,post:URLVariables=null,timeout:uint=15):void{
			if(_dataLoaderTimer){clearTimeout(_dataLoaderTimer);_dataLoaderTimer = 0;}
			if(timeout){_dataLoaderTimer = setTimeout(dataLoaderClose ,timeout*1000);}
			if(get != null){
				url += "&"+get.toString();
			}
			var request:URLRequest = new URLRequest(url);
			if(post != null){
				request.method = URLRequestMethod.POST;
				request.data = post;
			}
			_dataLoader.load(request);
		}
		private function loadRequest(url:String,isBit:Boolean=false,isPost:Boolean=false,data:URLVariables=null):void{
			var request:URLRequest = new URLRequest(url);
			if(isPost){request.method = URLRequestMethod.POST;}else{request.method = URLRequestMethod.GET;}
			if(data){request.data = data;}
			if(isBit){_fileLoader.dataFormat = URLLoaderDataFormat.BINARY}else{_fileLoader.dataFormat = URLLoaderDataFormat.TEXT;}
			_fileLoader.load(request);
		}
		
		private function dataSuccessHandler(event:Event):void{
			if(_dataLoaderTimer){clearTimeout(_dataLoaderTimer);_dataLoaderTimer = 0;}
			connectSuccess(event.target.data);
		}
		private function fileSuccessHandler(event:Event):void{
			if(_fileLoaderTimer){clearTimeout(_fileLoaderTimer);_fileLoaderTimer = 0;}
			connectSuccess(event.target.data);
		}
		private function imageLoaderSuccessHandler(event:Event):void{
			if(_imageLoaderTimer){clearTimeout(_imageLoaderTimer);_imageLoaderTimer = 0;}
			connectSuccess(_imageLoader.content);
		}
		private function classLoaderSuccessHandler(event:Event):void{
			if(_classLoaderTimer){clearTimeout(_classLoaderTimer);_classLoaderTimer = 0;}
			connectSuccess(_classLoader.contentLoaderInfo.applicationDomain);
		}
		
		private function progressHandler(event:ProgressEvent):void{
			connectProgress(event);
		}
		private function faultHandler(event:IOErrorEvent):void{
			if(_dataLoaderTimer){clearTimeout(_dataLoaderTimer);_dataLoaderTimer = 0;}
			connectError(event.text);
		}
		private function dataLoaderClose():void{
			_dataLoader.close();
			faultHandler(new IOErrorEvent(IOErrorEvent.IO_ERROR,false,false,Language.LoadTimeoutLabel));
		}
	}
}